package com.ugrow.ugrowcommunity.activity.moments.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.activity.moments.commentBean.Children;
import com.ugrow.ugrowcommunity.databinding.ChildCommentItemBinding;

import java.util.ArrayList;
import java.util.List;

public class ChildCommendAdapter extends RecyclerView.Adapter<ChildCommendAdapter.ViewHolder> {
    private List<Children> list;

    public ChildCommendAdapter(List<Children> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.child_comment_item, parent, false);
        ChildCommentItemBinding bind = DataBindingUtil.bind(view);
        return new ViewHolder(bind);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Children data = list.get(position);
        if (data != null) {
            holder.mBinding.mTvNickName.setText(data.getAuthorId());
            holder.mBinding.mTvContent.setText(data.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        ChildCommentItemBinding mBinding;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        public ViewHolder(@NonNull ChildCommentItemBinding bind) {
            super(bind.getRoot());
            mBinding = bind;
        }
    }

    public void dispatchUpdate(List<Children> newList) {
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallBack<>(list, newList), true);
        diffResult.dispatchUpdatesTo(this);
        list = newList;
    }

    public void addChildHeaderView(String content, String id) {
        Children data = new Children();
        data.setContent(content);
        data.setAuthorId(id);
        data.setLevel(1);
        ArrayList<Children> list1 = new ArrayList<>(list);
        list1.add(0, data);
        dispatchUpdate(list1);
    }
}
